import Vue from 'vue'
import Vuex from 'vuex';

Vue.use(Vuex)

const debug = process.env.NODE_ENV !== 'production'

const moduleA = {
    namespaced: true,
    state: {
        counter: 100,
    }, 
    mutations: {
        //递增  
        increase(state) {
            console.log("moduleA store-increase");
            state.counter++
        },
        //递减
        decrement(state, playload) {
            state.counter--;
            console.log('decrement playlaod: ' + JSON.stringify(playload));
        }
    },
    //异步
    actions: {
        increaseAction(context, payload) {
            setTimeout(() => {
                context.commit('increase', payload)
            }, 2000)
        },
        decrementAction(context, payload) {
            setTimeout(() => {
                context.commit('decrement', payload)
            }, 2000)
        } 
    },
    //计算属性
    getters: {
        doubleCounter(state) {
            return state.counter * state.counter;
        }
    }
}
const moduleB = {
    namespaced: true,
    state: {
        counter: 200,
    },
    mutations: {
        //递增  
        increase(state) {
            console.log("moduleB store-increase");
            state.counter++
        },
        //递减 
        decrement(state, playload) {
            state.counter--;
            console.log('decrement playlaod: ' + JSON.stringify(playload));
        }
    },
    //异步
    actions: {
        increaseAction(context, payload) {
            setTimeout(() => {
                context.commit('increase', payload)
            }, 2000)
        },
        decrementAction(context, payload) {
            setTimeout(() => {
                context.commit('decrement', payload)
            }, 2000)
        }
    },
    //计算属性
    getters: {
        doubleCounter(state) {
            return state.counter * state.counter* state.counter;
        }
    }
}

const userLogin  = {
    namespaced: true,
    state:{
        user:{
            token:'',
            userId:'',
            isLogin:false
        }
    },
    actions:{

    }
}



const store = new Vuex.Store({
    state: {
        counter: 1000,
        userName: '',
        userToken: ''
    },
    mutations: {
        //递增
        increase(state) {
            console.log("root store-increase");
            state.counter++
        },
        //递减
        decrement(state) {
            state.counter--
        }
    },
    actions: {
        increaseAction(context) {
            setTimeout(function(){
                //action通过提交mutation改变共享数据状态
               　context.commit('increase');
            },3000)
        },
        decrementAction(context){
            setTimeout(function(){
                //action通过提交mutation改变共享数据状态
               　　　　 context.commit('decrement');
            },3000)
        },
        //保存用户信息
        setUserInfo(context,payload){
            //先判断类型
            if(payload){
                let value = '';
                if (typeof payload === 'object') {
                    value = JSON.stringify(payload);
                } else {
                    value = payload;
                }
                console.log('setUserInfo set  USERINFO_KEY: ' + value);
                localStorage.setItem("USERINFO_KEY", value);
            }else{
                console.log('setUserInfo remove USERINFO_KEY : ' );
                localStorage.removeItem("USERINFO_KEY");
            }
        },
        setUserToken(context,payload){
            if(payload){//保存token
                let value = '';
                if (typeof payload === 'object') {
                    value = JSON.stringify(payload);
                } else {
                    value = payload;
                } 
                console.log('setUserToken  USER_TOKEN_KEY: ' + value);
                localStorage.setItem("USER_TOKEN_KEY", value);
            }
        }
    },
    getters: {
        doubleCounter(state) {
            return state.counter +=10;
        },
        userInfo(state){
            return JSON.parse(localStorage.getItem('USERINFO_KEY'));
        },
        userToken(state){
            return  JSON.parse(localStorage.getItem('USER_TOKEN_KEY'));
        }
    },
    modules: {
        a: moduleA,
        b: moduleB,
        login:userLogin
    }
})


export default store